package com.winhxd.b2c.search.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.vo.ProductVO;
import com.winhxd.b2c.common.domain.search.dto.ProductSearchParam;
import com.winhxd.b2c.common.domain.search.dto.SearchResult;
import com.winhxd.b2c.common.domain.search.dto.SkuChanged;
import com.winhxd.b2c.common.domain.search.model.ProductDocument;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.search.ProductSearchClient;
import com.winhxd.b2c.search.service.ProductSearchService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProductSearchController implements ProductSearchClient {
    @Resource
    private ProductSearchService productSearchService;

    @Override
    public ResponseResult<SearchResult<ProductDocument>> search(@RequestBody ProductSearchParam searchParam) {
        if (searchParam.getPriceProgramId() == null) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        SearchResult<ProductDocument> result = productSearchService.search(searchParam);
        return new ResponseResult<>(result);
    }

    @Override
    public ResponseResult<Boolean> importProduct(@RequestBody List<ProductVO> productVOList) {
        if (CollectionUtils.isEmpty(productVOList)) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        productSearchService.importProduct(productVOList);
        return new ResponseResult<>(true);
    }

    @Override
    public ResponseResult<Boolean> deleteSpuList(@RequestBody List<Long> spuIds) {
        if (CollectionUtils.isEmpty(spuIds)) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        productSearchService.deleteSpuList(spuIds);
        return new ResponseResult<>(true);
    }

    @Override
    public ResponseResult<Boolean> updateSku(@RequestBody List<SkuChanged> changes) {
        if (CollectionUtils.isEmpty(changes)) {
            throw new BusinessException(BusinessCode.CODE_1007);
        }
        productSearchService.updateSku(changes);
        return new ResponseResult<>(true);
    }
}
